package com.ih.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ih.pojo.Customer;
import com.ih.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/cust")
@Slf4j
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /**
     * 查询所有
     *
     * @param model
     * @return
     */
    /*@RequestMapping("/listCust")  //不在使用
    public String listCust(Model model) {
        List<Customer> customers = customerService.listCustomer();
        model.addAttribute("customers", customers);
        return "custmerList";
    }*/

    /**
     * 模糊查询带分页
     * @param pageNum
     * @param pageSize
     * @param customer
     * @param model
     * @return
     */
    @RequestMapping("/listCust")
    public String listCust(@RequestParam(required = false,defaultValue = "1",value = "pageNum") Integer pageNum,
                           @RequestParam(required = false,defaultValue = "10",value = "pageSize") Integer pageSize, Customer customer,Model model) {
        if (pageNum==null||pageNum<=0 || pageNum.equals("")){
            pageNum=1;
        }
        if (pageSize==null||pageSize<=0 || pageSize.equals("")){
            pageSize=10;
        }
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Customer> pageInfo = customerService.listCust(customer, pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        return "custmerList";
    }


    /**
     * 点击“添加”按钮跳转到“添加客户”页面
     *
     * @return “添加客户”页面
     */
    @RequestMapping("/preSaveCust")
    public String preSaveCust() {
        return "custSave";
    }

    /**
     * 添加客户信息，将表单输入的客户信息持久化到数据库中，添加成功返回客户列表页面
     *
     * @param customer
     * @return
     */
    @RequestMapping("/saveCust")
    public String saveCust(Customer customer) {
        log.info("Customer = " + customer);
        int rows = customerService.saveCustomer(customer);
        log.info("save customer row = " + rows);
        // 重定向到客户列表，将新添加的客户信息显示到客户列表
        return "redirect:/cust/listCust";
    }

    @RequestMapping("/delCust/{cid}")
    public String deleteCust(@PathVariable("cid") Integer cid) {
        int i = customerService.deleteCustomerById(cid);
        log.info("" + i);
        return "redirect:/cust/listCust";
    }

    /**
     * 根据id 修改做数据回显
     * @param cid
     * @param model
     * @return
     */
    @RequestMapping("/preUpdateCust/{cid}")
    public String preCustomerById(@PathVariable("cid") Integer cid,Model model){
        Customer customer = customerService.getCustomerById(cid);
        model.addAttribute("customer",customer);
        return "customerUpdate";
    }

    /**
     * 修改客户信息
     * @param customer
     * @return
     */
    @PostMapping("/updateCust")
    public String update(Customer customer){
        int i = customerService.updateCustomer(customer);
        return "redirect:/cust/listCust";
    }


}
